import { _decorator, Component, Node, game, view, macro, Game, CCBoolean, Prefab, instantiate, director, Camera } from 'cc';
import SNStateSyncAction from '../ngame/actions/statesync/SNStateSyncAction';
import ProtoAnyUtil from '../ngame/protobuf/ProtoAnyUtil';
import UIManage from '../ngame/ui/UIBase';
import { newnnode } from '../ngame/util/util-ngame';
import { ISceneController } from './controller/scene/ISceneController';
import GameConst, { NPrefabConst, NPrefabKeys, UIConfig, UI_VIEW } from './GameConst';
import { AStarManage } from './manage/AStarManage';
import { TouchsManage } from './manage/TouchsManage';
import CNWorld2Action from './ngame/actions/client/CNWorld2Action';
import SNWorld2Action from './ngame/actions/service/SNWorld2Action';
import NGameWorld2 from './ngame/NGameWorld2';
import NGameWorld2Chat from './ngame/NGameWorld2Chat';
import NSyncServer from './ngame/sync/NSyncServer';
import NGameMessageProtobuf from "../ngame/protobuf/NGameMessage/NGameMessage.js";
import { SNWorld2ChatAction } from './ngame/actions/service/SNWorld2ChatAction';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {

    //寻路
    @property({ type: AStarManage, displayName:'寻路'})
    aStar:AStarManage;

    //世界
    @property({ type: Node, displayName:'同步世界'})
    world:Node;

    //地图原始
    @property({ type: Prefab, displayName:'地图原始'})
    map:Prefab;

    //ui弹窗
    @property({ type: Node, displayName:'UI弹窗'})
    uiPopup:Node;

    //触碰控制器
    @property({ type: TouchsManage, displayName:'触碰管理器'})
    touchsManage:TouchsManage;

    //场景控制器
    @property({type: ISceneController,displayName:'场景控制器'})
    iSceneController:ISceneController;

    async onLoad(){

        // ProtoAnyUtil.addProto(NGameMessageProtobuf);
        // let name = (new Date).getTime().toString();
        // NGameWorld2Chat.getIns().start(name);

        // SNWorld2ChatAction.onProtobufMessage(NGameMessageProtobuf.NGameMessage.create({
        //     uid:123456
        // }));

        // return;

        GameConst.IsEditor = false;
        let name = (new Date).getTime().toString();
        GameConst.USER_ID = name;
        GameConst.TOUCHS_MANAGE = this.touchsManage;
        GameConst.I_SCENE = this.iSceneController;
        NGameWorld2Chat.getIns().start(name);
        NGameWorld2.getIns().start(name);

        UI_VIEW.POPUP = UIManage.getIns().add(this.uiPopup);

        // console.log("Main-onLoad",await SNWorld2Action.onSwitchRole("server"));

        let role = await NSyncServer.getIns().onSwitchRole();

        //切换失败则返回
        if(!role) {
            alert(`设置角色失败`)
            return
        }

        alert(`设置角色成功 [${role}]`)

        //加载同步
        await NSyncServer.getIns().load({
            world: this.onInitWorld(),
            isServer: role === "owner",
            nPrefabs: NPrefabConst,
            isLog:true,
        }).start();

        //加载寻路
        if(NSyncServer.getIns().info.isServer) this.aStar.init();
        console.log((<any>director.root));
        // //设置服务器不进行渲染
        // if(NSyncServer.getIns().info.isServer) {
        //     (<any>director.root).isRender = false;
        //     // director.getScene().getComponentsInChildren(Camera).forEach(camera => camera.enabled = false);
        // }

        // console.log(SNWorld2Action.nRelayEvent(GameConst.getUserId(),321654));
        
        // 向场景添加玩家
        NSyncServer.getIns().addServerNode(NPrefabKeys.玩家,{
            userId:GameConst.USER_ID
        });

    }

    //初始化场景
    onInitWorld(){
        
        console.log("Main-onInitWorld - 初始化场景");
        // instantiate(this.map).parent = this.world;
        return this.world;

    }

    start() {

    }

    update(deltaTime: number) {
        
    }
    
}

